#include"../source/server.hpp"
#include <iostream>

// void HandleClose(Channel* channel)
// {
//     std::cout << "close: " << channel->Fd() << std::endl;
//     DBG_LOG("close fd: %d", channel->Fd());
//     channel->Remove();//移除监控
//     delete channel;
// }

// void HandleWrite(Channel* channel)
// {
//     int fd = channel->Fd();
//     const char* data = "hellooooooo";
//     int ret = send(fd, data, strlen(data), 0);
//     if(ret < 0)
//     {
//         HandleClose(channel); //关闭释放
//         return;
//     }
//     channel->DisableWrite();//关闭写监控
// }

// void HandleError(Channel* channel)
// {
//     HandleClose(channel); //关闭释放
// }

// void HandleEvent(EventLoop* loop, Channel* channel, uint64_t timerid)
// {
//     loop->TimerRefresh(timerid);
// }

//管理所有的连接
std::unordered_map<uint64_t, PtrConnection> _conns;
uint64_t conn_id = 1;

void ConnectionDestory(const PtrConnection& conn)
{
    _conns.erase(conn->Id());
}

void OnConnected(const PtrConnection& conn)
{
    DBG_LOG("NEW CONNECTION:%p", conn.get());
}

void OnMessage(const PtrConnection& conn, Buffer* buf)
{
    DBG_LOG("%s", buf->ReadPosition());
    buf->MoveReadOffset(buf->ReadAbleSize());
    std::string str = "hello world";
    conn->Send(str.c_str(), str.size());
}

void Acceptor(EventLoop* loop, Channel* lst_channel)
{
    int fd = lst_channel->Fd();
    int newfd = accept(fd, NULL, NULL);
    if(newfd < 0)
        return;

    // uint64_t timerid = rand() % 10000;
    PtrConnection conn(new Connection(loop, conn_id, newfd));
    conn->SetMessageCallback(std::bind(OnMessage, std::placeholders::_1, std::placeholders::_2)); 
    conn->SetCloseCallback(std::bind(ConnectionDestory, std::placeholders::_1)); 
    conn->SetConnectedCallback(std::bind(OnConnected, std::placeholders::_1)); 
    conn->EnableInactiveRelease(10);//启动非活跃超时销毁功能
    conn->Established(); 

    _conns.insert(std::make_pair(conn_id, conn));

    conn_id++;

    //非活跃连接超时释放，十秒后关闭连接
    //定时销毁任务，必须在启动读事件之前
    // loop->TimerAdd(timerid, 10, std::bind(HandleClose, channel));

    // channel->EnableRead();
}
int main()
{
    // Poller poller;
    srand(time(NULL));
    EventLoop loop;
    Socket lst_sock;
    bool ret = lst_sock.CreateServer(8500);
    //为监听套接字创建一个channel 进行事件的管理
    Channel channel(&loop, lst_sock.Fd());
    channel.SetReadCallback(std::bind(Acceptor, &loop, &channel)); //回调函数中，获取新连接，为新连接创建channel并且添加监控
    channel.EnableRead(); //启动可读事件监控
    while(1)
    {
        loop.Start();

        // std::vector<Channel*> actives;
        // poller.Poll(&actives);
        // for(auto& a : actives)
        // {
        //     a->HandleEvent();
        // }
    }
    lst_sock.Close();

    // while(1)
    // {
    //     int newfd = lst_sock.Accept();
    //     if(newfd < 0)
    //     {
    //         continue;
    //     }
    //     Socket cli_sock(newfd);
    //     char buf[1024] = {0};
    //     int ret = cli_sock.Recv(buf, 1023);
    //     if(ret < 0)
    //     {
    //         cli_sock.Close();
    //         continue;
    //     }
    //     cli_sock.Send(buf, ret);
    //     cli_sock.Close();
    // }
    // lst_sock.Close();
    return 0;
}

